﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AvatarHelper;
using Avatar.Model;
using System.Data.Linq;

namespace Avatar.Person
{
   public static class FavoriteManager
    {
       /// <summary>
       /// 收藏职位信息
       /// </summary>
       /// <param name="_jobID"></param>
       /// <param name="_userName"></param>
       /// <returns></returns>
       public static bool Favorite(Guid _jobID,string _userName)
       {
           JobFavorites _jf=new JobFavorites();
           _jf.JobID=_jobID;
           _jf.User=_userName;
           _jf.Date=DateTime.Now;

           return DBHelper.Insert(_jf, j => j.User == _userName && j.JobID == _jobID, "收藏职位信息出错:");
       }

       public static JobFavorites GetFavorite(string _userName, Guid _jobID)
       {
           #region 入口检测

           if (string.IsNullOrEmpty(_userName)) throw new Exception("用户名不能为空");
           if (_jobID == Guid.Empty) throw new Exception("职位ID不能为空");

           #endregion

           return DBHelper.Get<JobFavorites>(f => f.User == _userName && f.JobID == _jobID, "获取职位收藏信息出错:");
       }

       /// <summary>
       /// 删除职位收藏信息
       /// </summary>
       /// <param name="_jobID"></param>
       /// <param name="_userName"></param>
       /// <returns></returns>
       public static bool DeleteAFavorite(Guid _jobID, string _userName)
       {
           //JobFavorites _jf = DBHelper.Get<JobFavorites>(j => j.JobID == _jobID && j.User == _userName, "获取职位信息出错:");

           //return DBHelper.Delete(_jf, "删除职位收藏出错:");

           throw new NotImplementedException();
       }

       public static bool DeleteAFavorite(int _id)
       {
           //JobFavorites _jf = DBHelper.Get<JobFavorites>(j => j.ID == _id, "获取职位信息出错:");

           //return DBHelper.Delete(_jf, "删除职位收藏出错:");

           using (var _adc = DBHelper.GetDataContext())
           {
               var _jf = _adc.JobFavorites.Where(j => j.ID == _id).FirstOrDefault();
               
               _adc.JobFavorites.DeleteOnSubmit(_jf);
               _adc.SubmitChanges();

               return true;
           }
       }

       /// <summary>
       /// 获取指定用户的收藏夹列表
       /// </summary>
       /// <param name="_userName"></param>
       /// <returns></returns>
       public static List<JobFavorites> GetFavorites(string _userName)
       {
           return DBHelper.GetAll<JobFavorites>(f => f.User == _userName, "获取职位信息出错:").OrderByDescending(f => f.Date).ToList();
       }
    }
}
